<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/7
  Time: 15:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<jsp:include page="include-head.jsp"></jsp:include>
<script src="layer/layer.js"></script>
<link rel="stylesheet" href="ztree/zTreeStyle.css">
<script src="ztree/jquery.ztree.all-3.5.min.js"></script>
<script src="crowdjs/my-role.js"></script>
<script type="text/javascript">
    $(function () {
        // 1.为分页操作准备初始化数据
        window.pageNum = 1;
        window.pageSize = 5;
        window.keyword = "";
        // 2.调用执行分页的函数，显示分页效果
        generatePage();

        // 3.关键字搜索按钮绑定查询操作
        $("#searchBtn").click(function () {
            var keyword = $("#keyword").val();
            window.keyword = keyword;
            generatePage();
        })

        // 4.点击新增按钮打开模态框
        $("#showAddModalBtn").click(function () {
            $('#addModal').modal('show');
        })

        // 5.新增模态框内，点击保存按钮执行ajax请求
        $("#saveBtn").click(function () {
            // 取前端输入的角色数据
            var roleName = $.trim($("#addModal [name=roleName]").val());
            // 发送ajax请求
            $.ajax({
                url: "role/save.json",
                type: "post",
                data: {
                    "name": roleName
                },
                dataType: "json",
                success: function (responseData) {
                    if (responseData.result == "SUCCESS") {
                        layer.msg("保存成功");
                        // 关闭模态框
                        $('#addModal').modal('hide');
                        // 重新加载页面，显示新增数据
                        // 将页码定位到最后一页
                        window.pageNum = 99999999;
                        generatePage();
                        $("#addModal [name=roleName]").val("");
                    }
                    if (responseData.result == "FAILED") {
                        layer.msg(responseData.message);
                    }
                },
                error: function (responseData) {
                    layer.msg(responseData.status + " " + responseData.statusText);
                }
            })

        })

        // 6.给页面上的“铅笔”按钮绑定单击响应函数，目的是打开模态框
        // 注意：这里的页面是通过js动态拼接而成的，如果使用click绑定，只能绑定初始页的按钮，如果翻页就无法绑定了
        // 此时需要使用jq里面的On绑定事件
        // on()函数的第一个参数是事件类型
        // on()函数的第二个参数是找到真正要绑定事件的元素的选择器
        // on()函数的第三个参数是事件的响应函数
        $("#rolePageBody").on("click", ".glyphicon-pencil", function () {
            // 打开edit的模态框
            $("#editModal").modal('show');
            // 获取roleName
            // 其实此时绑定的就是pencilBtn了，所以拿到这个文本，需要去他的父节点的前一个节点拿text
            var roleName = $(this).parent().parent().prev().text();
            window.roleId = this.id;
            console.log(roleId);
            $("#editModal [name=roleName]").val(roleName);
        })

        // 7.给更新模态框中的更新按钮绑定单击响应函数
        $("#updateBtn").click(function () {
            // 拿到修改后的roleName
            var roleName = $("#editModal [name=roleName]").val();
            // 发送ajax请求，就行更新
            $.ajax({
                url:"role/update.json",
                data:{
                    "id":window.roleId,
                    "name":roleName
                },
                type:"post",
                dataType:"json",
                success: function (responseData) {
                    if (responseData.result == "SUCCESS") {
                        layer.msg("更新成功");
                        // 关闭模态框
                        $('#editModal').modal('hide');
                        // 重新加载页面，显示新增数据
                        generatePage();
                    }
                    if (responseData.result == "FAILED") {
                        layer.msg(responseData.message);
                    }
                },
                error: function (responseData) {
                    layer.msg(responseData.status + " " + responseData.statusText);
                }
            })
        })

        // 8.点击确认模态框里面的删除按钮，执行删除操作
        $("#deleteBtn").click(function () {
                var requerstBody = JSON.stringify(window.roleIdArray);
            $.ajax({
                url:"role/remove.json",
                type:"post",
                data:requerstBody,
                dataType:"json",
                contentType:"application/json;charset=UTF-8",
                success: function (responseData) {
                    if (responseData.result == "SUCCESS") {
                        layer.msg("删除成功");
                        // 关闭模态框
                        $('#confirmModal').modal('hide');
                        // 重新加载页面
                        generatePage();
                    }
                    if (responseData.result == "FAILED") {
                        layer.msg(responseData.message);
                    }
                },
                error: function (responseData) {
                    layer.msg(responseData.status + " " + responseData.statusText);
                }
            })
        })

        // 9.单挑删除按钮绑定删除事件
        $("#rolePageBody").on("click", ".glyphicon-remove", function () {
            // 打开confirm的模态框
            $("#confirmModal").modal('show');
            // 获取roleName
            // 其实此时绑定的就是pencilBtn了，所以拿到这个文本，需要去他的父节点的前一个节点拿text
            var roleName = $(this).parent().parent().prev().text();
            var roleId = this.id;
            var roleArray = [{
                roleName:roleName,
                roleId:roleId
            }]
            console.log(roleArray);
            showConfirmModal(roleArray);
        })

        // 10.给全选按钮绑定响应函数
        $("#summaryBox").click(function () {
            // 获取全选框的状态
            var currentStatus = $("#summaryBox").prop("checked");
            // 点击后，让多选框与全选框的状态一致
            $(".itemBox").prop("checked",currentStatus);
        })

        // 11.若多选框全选了，全选框也要选中
        $("#rolePageBody").on("click", ".itemBox", function () {
            // 获取选中多选框数量
            var checkedBoxCount = $(".itemBox:checked").length;
            // 获取多选框的总数量
            var totalCount = $(".itemBox").length;
            // 比较，若相等，那么把全选框也选上
            $("#summaryBox").prop("checked",totalCount == checkedBoxCount)
        })

        // 12.批量删除按钮绑定函数
        $("#batchRemoveBtn").click(function () {
            var roleArray = [];
            // 遍历选中的多选框，取id
            $(".itemBox:checked").each(function(){
                var roleId = this.id;
                var roleName = $(this).parent().next().text();
                roleArray.push({
                    "roleId":roleId,
                    "roleName":roleName
                });
            })
            // 调用确认模态框
            showConfirmModal(roleArray);
        })

        // 13.点击权限按钮，显示树形结构供角色勾选权限
        $("#rolePageBody").on("click", ".glyphicon-check", function () {
            window.roleId = this.id;
            // 显示模态框
            $("#assignModal").modal("show");
            fillAuthTree();
        })

        // 14.分配模态框里面的确认按钮绑定事件
        $("#assignSaveBtn").click(function () {

            var authIdArray = [];
            // 1.获取zTree对象
            var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");
            // 2.zTree对象获取全部被勾选的节点getCheckedNodes()
            var checkedNodes = zTreeObj.getCheckedNodes();
            // 3.遍历checkedNodes，分别从node中拿到id，这个id就是authId
            for(var i = 0;i<checkedNodes.length;i++){
                var zTreeNode = checkedNodes[i];
                authIdArray.push(zTreeNode.id);
            }

            // 4.利用JSON.stringfy转为JSON数据传回后台
            // 为了让Map接收的类型都是List,把roleId也转成数组，这样就是Map<String,Integer>
            var requestBody = {
                "authIdArray":authIdArray,
                "roleId":[window.roleId]
            };
            requestBody = JSON.stringify(requestBody);
            // 5.发送ajax请求
            $.ajax({
                "url":"assign/do/role/assign/auth.json",
                "type":"post",
                "data":requestBody,
                "contentType":"application/json;charset=utf-8",
                "dataType":"json",
                "success":function (responseData) {
                    if(responseData.result == "FAILED"){
                        layer.msg(responseData.message);
                    }
                    
                    if(responseData.result == "SUCCESS"){
                        layer.msg("保存成功！");
                        $("#assignModal").modal("hide");
                    }
                },
                "error":function (responseData) {
                    layer.msg(responseData.status+" "+responseData.statusText);
                }
            })
        })
    })



</script>

<body>
<jsp:include page="include-nav.jsp"></jsp:include>


<div class="container-fluid">
    <div class="row">
        <jsp:include page="include-sidebar.jsp"></jsp:include>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keyword" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button type="button" id="batchRemoveBtn" class="btn btn-danger" style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <button id="showAddModalBtn" type="button" class="btn btn-primary" style="float:right;" ><i class="glyphicon glyphicon-plus"></i> 新增</button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">

                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"></div>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>s
    </div>
</div>


</body>
</html>
<jsp:include page="modal-role-add.jsp"></jsp:include>
<jsp:include page="modal-role-edit.jsp"></jsp:include>
<jsp:include page="modal-role-confirm.jsp"></jsp:include>
<jsp:include page="modal-role-assign-auth.jsp"></jsp:include>